//
//  SCImagesContentView.m
//  Shuaiche
//
//  Created by 党帅 on 2019/9/5.
//  Copyright © 2019 ShuaiChe. All rights reserved.
//

#import "SCImagesContentView.h"
#import "UIImageView+SCwebImage.h"
@interface SCImageControl : UIControl
@property(nonatomic, assign) BOOL canDelete;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *deleteButton;
@property(nonatomic, assign) BOOL isDelete;
@end

@implementation SCImageControl

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.layer.cornerRadius = 4;
        _imageView.layer.masksToBounds = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (void)setCanDelete:(BOOL)canDelete {
    if (canDelete) {
        self.deleteButton.hidden = NO;
    } else {
        _deleteButton.hidden = YES;
    }
}

- (void)deleteButtonClick {
    _isDelete = YES;
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}

- (UIButton *)deleteButton {
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setImage:[UIImage imageNamed:@"del_red_14"] forState:0];
        [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _deleteButton.imageEdgeInsets = UIEdgeInsetsMake(-5, 0, 0, 0);
        [self addSubview:_deleteButton];
    }
    return _deleteButton;
}

@end


@implementation SCImagesContentView

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        _rowNumber = 4;
        _lineSpace = 10;
        CGFloat w = CGRectGetWidth(self.frame);
        if (kSCScreenWidth == 320) {
            _itemWidth = 70;
            _itemSpace = 6;
        } else {
            _itemSpace = 10;
            _itemWidth = floorf((w - 30) / 4.f);
        }
    }
    return self;
}

- (void)cancelLoadCurrentImage {
    for (SCImageControl *image in self.subviews) {
//        [image.imageView sc_cancelImageLoad];
    }
}

- (void)setDataSource:(NSMutableArray *)dataSource {
    _dataSource = dataSource;
    [self setNeedsLayout];
    [self updateItems];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat w = CGRectGetWidth(self.frame);
    if (kSCScreenWidth == 320) {
        _itemWidth = 70;
        _itemSpace = 6;
    } else {
        _itemSpace = 10;
        _itemWidth = floorf((w - 30) / 4.f);
    }
}

- (void)setRowNumber:(NSInteger)rowNumber {
    _rowNumber = rowNumber;
}

- (void)imageControlClick:(SCImageControl *)control {
    if (control.isDelete) {
        [_dataSource removeObjectAtIndex:control.tag];
        [_delegate imageContentDelete];
        [self updateItems];
    } else {
        [_delegate imageContentShowBigImageIndex:control.tag];
    }
}

- (void)updateItems {
    NSInteger imageCount = _dataSource.count;
    NSInteger imageViewCount = self.subviews.count;
    CGFloat w = CGRectGetWidth(self.frame);
    float x = 0 , y = 0;
    if (imageCount >= imageViewCount) {
        for (int i = 0; i< imageCount; i++) {
            if (i < imageViewCount) {
                id tmp = _dataSource[i];
                SCCarAddImageItemModel *imageModel = nil;
                if ([tmp isKindOfClass:NSString.class]) {
                    imageModel = [[SCCarAddImageItemModel alloc] init];
                    imageModel.imageUrlPath = tmp;
                } else {
                    imageModel = tmp;
                }
                SCImageControl *control = self.subviews[i];
                control.tag = i;
                if (imageModel.image) {
                    control.imageView.image = imageModel.image;
                } else {
                    [control.imageView sc_setImageWithPath:sc_url_img(imageModel.imageUrlPath)];
                }
                x = CGRectGetMaxX(control.frame) + _itemSpace;
                y = CGRectGetMinY(control.frame);
            } else {
                SCImageControl *control = [[SCImageControl alloc] init];
                control.tag = i;
                [control addTarget:self action:@selector(imageControlClick:) forControlEvents:UIControlEventTouchUpInside];
                id tmp = _dataSource[i];
                SCCarAddImageItemModel *imageModel = nil;
                if ([tmp isKindOfClass:NSString.class]) {
                    imageModel = [[SCCarAddImageItemModel alloc] init];
                    imageModel.imageUrlPath = tmp;
                } else {
                    imageModel = tmp;
                }
                if (imageModel.image) {
                    control.imageView.image = imageModel.image;
                } else {
                    [control.imageView sc_setImageWithPath:sc_url_img(imageModel.imageUrlPath)];
                }
                if (x + _itemWidth > w) {
                    x = 0;
                    y = y + _itemWidth + _lineSpace;
                }
                
                if (_canDeleteImage) {
                    control.canDelete = YES;
                    control.frame = CGRectMake(x, y, _itemWidth, _itemWidth);
                    control.imageView.frame = CGRectMake(0, 10, _itemWidth - 10, _itemWidth - 10);
                    control.deleteButton.frame = CGRectMake(_itemWidth - 25, 0, 25, 25);
                } else {
                    control.frame = CGRectMake(x, y, _itemWidth, _itemWidth);
                    control.imageView.frame = CGRectMake(0, 0, _itemWidth, _itemWidth);
                }
                [self addSubview:control];
                x = x + _itemWidth + _itemSpace;
            }
        }
    } else {
        for (int i = 0; i < imageViewCount; i++) {
            SCImageControl *control = self.subviews[i];
            if (i < imageCount) {
                id tmp = _dataSource[i];
                SCCarAddImageItemModel *imageModel = nil;
                if ([tmp isKindOfClass:NSString.class]) {
                    imageModel = [[SCCarAddImageItemModel alloc] init];
                    imageModel.imageUrlPath = tmp;
                } else {
                    imageModel = tmp;
                }
                if (imageModel.image) {
                    control.imageView.image = imageModel.image;
                } else {
                    [control.imageView sc_setImageWithPath:sc_url_img(imageModel.imageUrlPath)];
                }
            } else {
                [control removeFromSuperview];
            }
        }
    }
    CGRect frame = self.frame;
    if (_dataSource.count > 0) {
        frame.size.height = y + _itemWidth;
    } else {
        frame.size.height = 0;
    }
    self.frame = frame;
}


@end
